package domain;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.Valid;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Access(AccessType.PROPERTY)
public class Auction extends DomainEntity {

	// Constructors

	public Auction() {
		super();
		bids = new HashSet<Bid>();
	}
	
	// Attributes
	
	private Date startMoment;
	private Date finishMoment;
	private double startingPrice;
	
	
	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = "yyyy/MM/dd HH:mm")
	public Date getStartMoment() {
		return startMoment;
	}
	
	public void setStartMoment(Date startMoment) {
		this.startMoment = startMoment;
	}
	
	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = "yyyy/MM/dd HH:mm")
	public Date getFinishMoment() {
		return finishMoment;
	}
	
	public void setFinishMoment(Date finishMoment) {
		this.finishMoment = finishMoment;
	}
	
	@Min(0)
	@Digits(integer=4, fraction=2)
	public double getStartingPrice() {
		return startingPrice;
	}
	public void setStartingPrice(double startingPrice) {
		this.startingPrice = startingPrice;
	}
	
	//Relationship
	
	private Customer creator;
	private Painting painting;
	private Collection<Bid> bids;
	private Bid winningBid;
	
	
	@NotNull
	@Valid
	@ManyToOne(optional = false)
	public Customer getCreator() {
		return creator;
	}
	public void setCreator(Customer creator) {
		this.creator = creator;
	}
	
	@NotNull
	@Valid
	@ManyToOne(optional = false)
	public Painting getPainting() {
		return painting;
	}
	public void setPainting(Painting painting) {
		this.painting = painting;
	}
	
	@NotNull
	@Valid
	@OneToMany(mappedBy="auction")
	public Collection<Bid> getBids() {
		return bids;
	}

	public void setBids(Collection<Bid> bids) {
		this.bids = bids;
	}
	
	@Valid
	@OneToOne(optional = true)
	public Bid getWinningBid() {
		return winningBid;
	}

	public void setWinningBid(Bid winningBid) {
		this.winningBid = winningBid;
	}
	
	
}
